package com.controller;


import com.entity.FUser;
import com.entity.table.FUserTableDef;
import com.mapper.FUserMapper;
import com.service.FUserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {
    @GetMapping("/login")
    public String toLoginPage(){
        return "client/login";
    }
    @Autowired
//    FUserMapper fUserMapper;
    FUserService fUserService;
    @PostMapping("/toLogin")//提交登录，登录成功跳转到后台首页
    public String login(FUser fUser, HttpSession httpSession, Model model) {
        FUser user = fUserService.queryChain().where(FUserTableDef.F_USER.USER_NAME.eq(fUser.getUserName()))
                .and(FUserTableDef.F_USER.PWD.eq(fUser.getPwd())).one();
        if (user != null) {
            httpSession.setAttribute("user", user);//将用户对象添加到Session
            model.addAttribute("user", user);
            model.addAttribute("msg", "账号或密码错误，请重新输入！");
//            return "client/index";
            return "redirect:/";//跳转到前台首页
        } else {
            model.addAttribute("msg", "账号或密码错误，请重新输入！");
            return "client/login";
        }
    }
}
